package com.zrx.app.android.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.zrx.app.android.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;

/**
 * Created by liuji on 2017/4/14.
 *
 *
 * 添加一张照片页面   内容分为标题关键字，照片描述和一个按钮
 */

public class ImageAppendableFrag extends BaseFragment {
    private static final String TAG = "ImageAppendableFrag";
    private View layout_append;
    View btnAppend = null;
    TextView label_keyName = null;//名称
    TextView label_description = null;//描述
    JSONObject elements;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {


        final View _view = inflater.inflate(R.layout.layout_image_appendable, container, false);
        try{
            layout_append =  _view.findViewById(R.id.layout_append);
            btnAppend = _view.findViewById(R.id.btn_append);
            label_keyName =(TextView) _view.findViewById(R.id.label_keyName);
            label_description =(TextView) _view.findViewById(R.id.label_description);

            JSONObject _eleArr = node.getJSONObject("val");//val->value
            if(_eleArr==null) {
                _eleArr=new JSONObject();
                node.put("val", _eleArr);
            }
            elements =_eleArr;


            btnAppend.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    JSONObject innerNode=new JSONObject();
                    String innerKey=key+ elements.size();
                    innerNode.put("type","照片");
                    elements.put(innerKey,innerNode);

                    FragmentManager fragMan = getChildFragmentManager();
                    FragmentTransaction fragTransaction = fragMan.beginTransaction();

                    ImageFrag myFrag = new ImageFrag();
                    myFrag.initParams(innerNode,innerKey,dir);

                    fragTransaction.add(layout_append.getId(), myFrag );
                    fragTransaction.commit();
                }
            });

            Set<String> keySet = elements.keySet();
            ArrayList<String> keyList=new ArrayList<>(keySet);
//        keyList.sort(String::compareTo);
            Collections.sort(keyList, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    int aNum=1000;
                    int bNum=1000;
                    try{
                        aNum=Integer.valueOf(o1.substring(key.length()));
                    }catch (Exception e){}
                    try{
                        bNum=Integer.valueOf(o2.substring(key.length()));
                    }catch (Exception e){}
                    return aNum-bNum;
                }
            });
            for(String innerKey:keyList){
                JSONObject innerNode= elements.getJSONObject(innerKey);

                FragmentManager fragMan = getChildFragmentManager();
                FragmentTransaction fragTransaction = fragMan.beginTransaction();

                ImageFrag myFrag = new ImageFrag();
                myFrag.initParams(innerNode,innerKey,dir);
                fragTransaction.add(layout_append.getId(), myFrag );
                fragTransaction.commit();

            }

            label_keyName.setText(key);
            if(node.containsKey("policy"))
                label_description.setText(node.getJSONArray("policy").toJSONString());
            else
                label_description.setText("");


        }catch (Exception e){
            e.printStackTrace();
        }

        return _view;
    }

    public String getKey() {
        return key;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK){

        }

    }
}
